import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Rights from '../components/power/Rights'
import Roles from '../components/power/Roles'
import Cate from '../components/goods/Cate'
import Goods from '../components/goods/Goods'
import Add from '../components/goods/Add'
import Params from '../components/goods/Params'
import Orders from '../components/order/Order'

//  shop端的页面
// import ShopHome from '../components/Shop/ShopHome'
import Cart from '../components/shop_cart/Cart'
import Shop from '../components/Shop/Shop'
import ShopLogin from '../components/Shop/ShopLogin'
import ShopRegister from '../components/Shop/ShopRegister'
import GoodsDetail from '../components/Shop/GoodsDetail'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          component: Welcome
        },
        {
          path: '/users',
          component: Users
        },
        {
          path: '/rights',
          component: Rights
        },
        {
          path: '/roles',
          component: Roles
        },
        {
          path: '/categories',
          component: Cate
        },
        {
          path: '/goods',
          component: Goods

        },
        {
          path: '/goods/add',
          component: Add
        },
        {
          path: '/params',
          component: Params
        },
        {
          path: '/orders',
          component: Orders
        }
      ]
    },
    //  shop端的路由
    {
      path: '/shop',
      redirect: '/shop_home'
    },
    {
      path: '/shop_home',
      component: Shop
    },
    {
      path: '/cart',
      component: Cart
    },
    {
      path: '/shop_login',
      component: ShopLogin
    },
    {
      path: '/shop_register',
      component: ShopRegister
    },
    {
      path: '/goods_detail',
      component: GoodsDetail
    }
  ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转

  if (to.path === '/login') return next()
  //  如果是登陆客户端 直接放行
  // if (to.path === '/shop') return next()
  if (to.path === '/shop_home') return next()
  if (to.path === '/shop_login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
